package com.wanka.jutou.factorybean;

import java.util.Properties;

/**
 * @author yangqing
 * @version 1.0
 * @date 2025/9/1 17:20
 * @description
 */
public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource> {
    private String serverAddr;
    private String contextPath;
    private String username;
    private String password;
    private String groupId;
    private String dataId;
    private Converter converter;
    private String endpoint;
    private String namespace;
    private String accessKey;
    private String secretKey;

    public NacosDataSource getObject() throws Exception {
        Properties properties = new Properties();
        if (!StringUtils.isEmpty(this.serverAddr)) {
            properties.setProperty("serverAddr", this.serverAddr);
        } else {
            properties.setProperty("endpoint", this.endpoint);
        }

        if (!StringUtils.isEmpty(this.contextPath)) {
            properties.setProperty("contextPath", this.contextPath);
        }

        if (!StringUtils.isEmpty(this.accessKey)) {
            properties.setProperty("accessKey", this.accessKey);
        }

        if (!StringUtils.isEmpty(this.secretKey)) {
            properties.setProperty("secretKey", this.secretKey);
        }

        if (!StringUtils.isEmpty(this.namespace)) {
            properties.setProperty("namespace", this.namespace);
        }

        if (!StringUtils.isEmpty(this.username)) {
            properties.setProperty("username", this.username);
        }

        if (!StringUtils.isEmpty(this.password)) {
            properties.setProperty("password", this.password);
        }

        return new NacosDataSource(properties, this.groupId, this.dataId, this.converter);
    }

    public Class<?> getObjectType() {
        return NacosDataSource.class;
    }

    public String getServerAddr() {
        return this.serverAddr;
    }

    public void setServerAddr(String serverAddr) {
        this.serverAddr = serverAddr;
    }

    public String getContextPath() {
        return this.contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGroupId() {
        return this.groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getDataId() {
        return this.dataId;
    }

    public void setDataId(String dataId) {
        this.dataId = dataId;
    }

    public Converter getConverter() {
        return this.converter;
    }

    public void setConverter(Converter converter) {
        this.converter = converter;
    }

    public String getEndpoint() {
        return this.endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getNamespace() {
        return this.namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getAccessKey() {
        return this.accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return this.secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
}
